package adapters;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.administrator.newxlmedical.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import beans.PutAndDeleteBean;
import beans.SearchBean;
import beans.StorageWarningBean;
import beans.StorageWarningInfoBean;
import beans.WarningInfoBean;
import butterknife.BindView;
import constants.HandlerConstant;
import custom.BaseRecyclerAdapter;
import customhandler.MyHandler;
import dialogs.StorageWarningModifyDialog;
import netframe.GetRetrofit;
import netframe.NetAddress;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import singleton.SingletonClass;
import utils.ToastUtil;

/**
 * Created by Administrator on 2018/5/7.
 */

public class SearchAdapter extends BaseRecyclerAdapter<SearchAdapter.SearchViewHolder> {

    private Context mContext;
    private LayoutInflater inflater;
    private List<StorageWarningInfoBean> list;
//    private OnClickListener onClickListener;
    private MyHandler allHandler;

    public SearchAdapter(Context mContext, List<StorageWarningInfoBean> list) {
        this.list = list;
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
        allHandler = SingletonClass.getSingleton(MyHandler.class);
    }

    public List<StorageWarningInfoBean> getList(){
        return list;
    }

    @Override
    public SearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        SearchViewHolder viewHolder = new SearchViewHolder(inflater.inflate(R.layout.adapter_search, parent, false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final SearchViewHolder viewHolder, final int position) {
        final StorageWarningInfoBean searchBean = list.get(position);
        viewHolder.tvName.setText(searchBean.getGoodsName());
        viewHolder.tvSpecification.setText(searchBean.getSpecification());
        viewHolder.tvNumber.setText(searchBean.getNumber());
        viewHolder.tvUnit.setText(searchBean.getUnit());
        viewHolder.tvSuppier.setText(searchBean.getSupplier());
        viewHolder.tvCabinetNumber.setText(searchBean.getWarnCount());
        if (position % 2 == 0) {
            viewHolder.llTitle.setBackgroundColor(mContext.getResources().getColor(R.color.partLineBackground));
        } else {
            viewHolder.llTitle.setBackgroundColor(mContext.getResources().getColor(R.color.white));
        }

        viewHolder.tvModify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StorageWarningModifyDialog modifyDialog = new StorageWarningModifyDialog(mContext, R.style.dialog,"修改预警值" , searchBean.getWarnCount(), new StorageWarningModifyDialog.ModifyInterface() {
                    @Override
                    public void modify(final String number) {
                        GetRetrofit.getInstance().putStorageWarningInfo(NetAddress.token, NetAddress.machineCode, "set WarningCount = " + number + " where GoodsInfoId = " + searchBean.getGID()+ " and TerminalInfoId = " + NetAddress.terminalInfoId)
                                .enqueue(new Callback<PutAndDeleteBean>() {
                                    @Override
                                    public void onResponse(Call<PutAndDeleteBean> call, Response<PutAndDeleteBean> response) {
                                        Log.e("aaa", "onResponse: "+ searchBean.getGID() + NetAddress.terminalInfoId );
                                        PutAndDeleteBean body = response.body();
                                        StorageWarningInfoBean bean = list.get(position);
                                        if (body != null) {
                                            if (Integer.parseInt(bean.getWarnCount()) >= Integer.parseInt(bean.getNumber())){
                                                Message message = allHandler.obtainMessage();
                                                message.what = HandlerConstant.STORAGEWARNING_MODIFY;
                                                if (Integer.parseInt(number) < Integer.parseInt(bean.getNumber())){
                                                    message.obj = false;
                                                }else {
                                                    if (Integer.parseInt(number) == 0 && Integer.parseInt(bean.getNumber()) == 0){
                                                        message.obj = false;
                                                    }else {
                                                        if (Integer.parseInt(bean.getNumber()) == 0) {
                                                            message.obj = true;
                                                        }
                                                    }
                                                }
                                                allHandler.sendMessage(message);
                                            }else {
                                                if (Integer.parseInt(number) >= Integer.parseInt(bean.getNumber())){
                                                    Message message = allHandler.obtainMessage();
                                                    message.what = HandlerConstant.STORAGEWARNING_MODIFY;
                                                    message.obj = true;
                                                    allHandler.sendMessage(message);
                                                }
                                            }
                                            ToastUtil.showToast(mContext, "修改成功");
                                            list.get(position).setWarnCount(number);
                                            notifyDataSetChanged();
                                        } else {
                                            ToastUtil.showToast(mContext, "修改失败");
                                        }
                                    }

                                    @Override
                                    public void onFailure(Call<PutAndDeleteBean> call, Throwable t) {

                                    }
                                });
                    }
                });
                modifyDialog.setCanceledOnTouchOutside(false);
                modifyDialog.show();

            }
        });

//        viewHolder.tvName.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                onClickListener.onShowDetail(position,viewHolder.tvName,210);
//                return true;
//            }
//        });
//
//        viewHolder.tvSpecification.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                onClickListener.onShowDetail(position,viewHolder.tvSpecification,390);
//                return true;
//            }
//        });
//
//        viewHolder.tvSuppier.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                onClickListener.onShowDetail(position,viewHolder.tvSuppier,610);
//                return true;
//            }
//        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class SearchViewHolder extends BaseRecyclerAdapter.BaseViewHolder {
        @BindView(R.id.tv_search_name)
        TextView tvName;
        @BindView(R.id.tv_search_specification)
        TextView tvSpecification;
        @BindView(R.id.tv_search_suppier)
        TextView tvSuppier;
        @BindView(R.id.tv_search_number)
        TextView tvNumber;
        @BindView(R.id.tv_search_unit)
        TextView tvUnit;
        @BindView(R.id.tv_search_cabinet_number)
        TextView tvCabinetNumber;
        @BindView(R.id.ll_search_adapter)
        LinearLayout llTitle;
        @BindView(R.id.tv_search_modify)
        TextView tvModify;

        public SearchViewHolder(View itemView) {
            super(itemView);
        }
    }

//    public void setOnClickListener(OnClickListener onClickListener) {
//        this.onClickListener = onClickListener;
//    }
//
//    public interface OnClickListener {
//        //        void onShowDetailSpecification(int position, TextView view);
//        void onShowDetail(int position, TextView view,int xOffWidth);
//        //        void onShowDetailSupplier(int position, TextView view);
//    }
}
